package com.hougong.lineclient;


import com.hougong.lineclient.common.GlobalCode;
import com.hougong.lineclient.common.GlobalData;
import com.hougong.lineclient.controller.BootController;
import com.hougong.lineclient.entity.MyFriendVo;
import com.hougong.lineclient.notice.ApplyAddFriendNotice;
import com.hougong.lineclient.notice.FriendMessageNotice;
import com.hougong.lineclient.notice.OnlineStatusNotice;
import com.hougong.lineclient.response.GroupMessageNotice;
import com.hougong.lineclient.response.LoginResponse;
import com.hougong.lineclient.transport.BaseFrame;
import com.hougong.lineclient.transport.HeartBeatTask;
import com.hougong.lineclient.utils.DateUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.lang.reflect.InvocationTargetException;
import java.util.Date;


/**
 * 事件分发
 */
@Component
public class Dispather {

    private static final Logger LOGGER=LoggerFactory.getLogger(Dispather.class);

    @Autowired
    private BootController bootController;

    public   void handler(BaseFrame baseFrame, Channel channel) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        LOGGER.debug("baseframe:"+baseFrame);
        short bizType = baseFrame.getBizType();
        BaseBodyCodec baseBodyCodec=null;
        ByteBuf byteBuf = Unpooled.wrappedBuffer(baseFrame.getBody());
        if(bizType==GlobalCode.Login_Response.getBizCode()){
            baseBodyCodec=new LoginResponse().fillBodyPojo(byteBuf);
            LoginResponse loginResponse=(LoginResponse)baseBodyCodec;
            if(loginResponse.getFlag()==LoginResponse.Success_Flag){
                //登录成功
                GlobalData.uid=loginResponse.getUid();
                GlobalData.token=((LoginResponse) baseBodyCodec).getToken();

                HeartBeatTask.getInstance().startTask();

//                LoginUserRepository userRepository = (LoginUserRepository) SpringContextUtils.getBean(LoginUserRepository.class);
//                LoginUser loginUser=new LoginUser();
//                loginUser.setId(loginResponse.getUid());
//                loginUser.setUserName(GlobalData.inputUserName);
//                loginUser.setToken(GlobalData.token);
//                userRepository.save(loginUser);
                System.out.println("----   登录成功    ----");
                System.out.println("----正在获取好友列表----");
                bootController.mainViewListAction();
            }else{
                GlobalData.clearData();
                System.out.println("用户名或密码错误!");
                bootController.bootMenu();
            }
        }else if(bizType==GlobalCode.Friend_Message_Notice.getBizCode()){
            //好友来了信息
            baseBodyCodec=new FriendMessageNotice().fillBodyPojo(byteBuf);
            FriendMessageNotice friendMessageNotice=(FriendMessageNotice)baseBodyCodec;
            MyFriendVo myFriendVo = GlobalData.mapMyFriend.get(friendMessageNotice.getFid());
            if(myFriendVo==null){
                bootController.getMyFriendListRequest();
                myFriendVo=GlobalData.mapMyFriend.get(friendMessageNotice.getFid());
            }
            if(GlobalData.friend==null){
                System.out.println("您的好友(" +myFriendVo.getUsername()+")来了新消息:"+friendMessageNotice.getMsg());
            }else{
                if(friendMessageNotice.getFid()==GlobalData.friend.getUid()){
                    System.out.println(GlobalData.friend.getUsername()+" : "+friendMessageNotice.getMsg());
                }else {
                    System.out.println("您的好友(" +myFriendVo.getUsername()+")来了新消息:"+friendMessageNotice.getMsg());
                }
            }
        }else if(bizType==GlobalCode.Other_Apply_Add_You_Notice.getBizCode()){
            //申请添加
            baseBodyCodec=new ApplyAddFriendNotice().fillBodyPojo(byteBuf);
            ApplyAddFriendNotice applyAddFriendNotice= (ApplyAddFriendNotice) baseBodyCodec;
            System.out.println("--- 有个用户"+applyAddFriendNotice.getUserName()+"申请添加你为好友 ---");
        }else if(bizType==GlobalCode.Friend_OnLine_Status_Notice.getBizCode()){
            baseBodyCodec=new OnlineStatusNotice().fillBodyPojo(byteBuf);
            OnlineStatusNotice onlineStatusNotice= (OnlineStatusNotice) baseBodyCodec;
            System.out.println("您的好友"+GlobalData.mapMyFriend.get(onlineStatusNotice.getFid()).getUsername()+"已上线(*^▽^*)");
        }else if(bizType==GlobalCode.Group_Message_Notice.getBizCode()){
            baseBodyCodec=new GroupMessageNotice().fillBodyPojo(byteBuf);
            GroupMessageNotice groupMessageNotice= (GroupMessageNotice) baseBodyCodec;
            long gid = groupMessageNotice.getGid();
            if(GlobalData.gid!=null &&gid==GlobalData.gid){
                    //当前正在该群
                System.out.println(groupMessageNotice.getUserName()+" : "+groupMessageNotice.getMsg()+"   ------"
                     +DateUtils.format(new Date(groupMessageNotice.getTimestamp()),"HH:mm:ss"));
             }
        }
    }
}
